It is possible to name the days 0 through 6, where day 0 is Sunday and day 6 is Saturday. If you go on a wonderful holiday leaving on day number 3 (a Wednesday) and you return home after 10 nights, you arrive home on day number 6 (a Saturday).
Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the number of day of the week you will return on.
In [8]:
days_of_week = [
# 0 1 2
'Sunday', 'Monday', 'Tuesday',
# 3 4 5
'Wednesday', 'Thursday', 'Friday',
# 6
'Saturday',
]
# Gather user input
# Need to use the `int` call so that it'll correctly be
# an integer for mathmatical operations
leaving_day = int(input('What day are you leaving? '))
length_of_vacation = int(input('How long will you be gone? '))
# Get the day we will hit, it may be larger than 7
final_day = length_of_vacation + leaving_day
# Get the index for the day of the week
# By using the modulo operator we can take advantage of
# knowing the remainder of any numbers.
# For example if the day is 2 and our stay is 5
# Then we will have 7, which means we leave on a Tuesday
# and get back on a Sunday.
final_day_index = final_day % len(days_of_week)
print(days_of_week[final_day_index], final_day_index)
In [9]:
"""
Studio: Holiday
"""
# get input: departure day (0-6) and duration
departure_day = input("Which day are you leaving on? (0=Sun, 1=Mon, etc)")
departure_day = int(departure_day)
duration = input("How many days will you be done?")
duration = int(duration)
# calculate return day and respond
return_day = (departure_day + duration) % 7
print("You will return on day", return_day)
In [10]:
day = int(input("What day will you leave?"))
travelDays = int(input("How many days will you be gone?"))
newDay = day + travelDays
newDay = newDay % 7
print("You will return on", newDay)
In [ ]: